package com.lq.ecoldchain.ui.mine.carInfo.typelist

import android.app.Activity
import android.arch.lifecycle.MutableLiveData
import android.content.Intent
import android.view.LayoutInflater
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.common.CarTypeRecyclerAdapter
import com.lq.ecoldchain.entity.CarType

/*
*2018/12/28 0028  14:27
*车型 by lq
*/
class CarTypeViewModel:BaseViewModel(){
    val list by lazy { ArrayList<CarType>() }
    val adapter by lazy { CarTypeRecyclerAdapter(list) }
    val refreshing by lazy { MutableLiveData<Boolean>() }

    init {
        obtainCarTypeData()
        initAdapter()
    }

    private fun initAdapter(){
        adapter.setOnItemChildClickListener { _, view, position ->
            val activity = view.context as Activity
            //ARouter startActivity For Result
            val resultIntent = Intent()
            resultIntent.putExtra("data",list[position])
            activity.setResult(-1,resultIntent)
            activity.finish()
        }
    }

     fun refreshingData(){
         refreshing.value = true
        obtainCarTypeData()
    }

    private fun obtainCarTypeData(){
        BaseNetCore.ApiServiceImpl.obtainCarTypeData().compose(RxTransformer.switchSchedulers()).subscribe{t->
            if(t!=null && !t.data.isNullOrEmpty()){
                list.clear()
                list.addAll(t.data)
                refreshing.value = false
                adapter.notifyDataSetChanged()
            }
        }
    }
}